package com.edu.system.service.impl;

import com.edu.system.domain.ExamDetail;
import com.edu.system.domain.TExamMiss;
import com.edu.system.domain.dto.StatisticsExamDTO;
import com.edu.system.mapper.ExamDetailMapper;
import com.edu.system.service.ITExamMissService;
import com.edu.system.service.ITStudyScoreService;
import java.util.List;
import com.edu.common.utils.DateUtils;
import java.util.Optional;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.TExaminationMapper;
import com.edu.system.domain.TExamination;
import com.edu.system.service.ITExaminationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 考试设置Service业务层处理
 * 
 * @author xxxx
 * @date 2024-01-05
 */
@Service
public class TExaminationServiceImpl implements ITExaminationService 
{
    @Autowired
    private TExaminationMapper tExaminationMapper;

    @Resource
    private ITExamMissService itExamMissService;

    @Resource
    private ExamDetailMapper examDetailMapper;
    @Resource
    private ITStudyScoreService itStudyScoreService;

    /**
     * 查询考试设置
     * 
     * @param id 考试设置主键
     * @return 考试设置
     */
    @Override
    public TExamination selectTExaminationById(Long id)
    {
        return tExaminationMapper.selectTExaminationById(id);
    }

    @Override
    public TExamination selectTExaminationByTitle(String title) {
        TExamination e = new TExamination();
        e.setTitle(title);
        return Optional.ofNullable(tExaminationMapper.selectTExaminationList(e))
            .filter(CollectionUtils::isNotEmpty)
            .map(a -> a.get(0))
            .orElseGet(() -> null);
    }

    /**
     * 查询考试设置列表
     * 
     * @param tExamination 考试设置
     * @return 考试设置
     */
    @Override
    public List<TExamination> selectTExaminationList(TExamination tExamination)
    {
        return tExaminationMapper.selectTExaminationList(tExamination);
    }

    /**
     * 新增考试设置
     * 
     * @param tExamination 考试设置
     * @return 结果
     */
    @Override
    public int insertTExamination(TExamination tExamination)
    {
        return tExaminationMapper.insertTExamination(tExamination);
    }

    /**
     * 修改考试设置
     * 
     * @param tExamination 考试设置
     * @return 结果
     */
    @Override
    public int updateTExamination(TExamination tExamination)
    {
        tExamination.setUpdateTime(DateUtils.getNowDate());
        return tExaminationMapper.updateTExamination(tExamination);
    }

    /**
     * 批量删除考试设置
     * 
     * @param ids 需要删除的考试设置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTExaminationByIds(Long[] ids)
    {
        itStudyScoreService.deleteByExamId(ids);
        return tExaminationMapper.deleteTExaminationByIds(ids);
    }

    /**
     * 删除考试设置信息
     * 
     * @param id 考试设置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTExaminationById(Long id)
    {
        itStudyScoreService.deleteByExamId(new Long[]{id});
        return tExaminationMapper.deleteTExaminationById(id);
    }

    @Override
    public StatisticsExamDTO examStat(String cohort, Long examId, String lessonName) {

        long planCnt = examDetailMapper.countByExam(ExamDetail.builder().cohort(cohort).examId(examId).lessonName(lessonName).build());
        List<TExamMiss> tExamMisses = itExamMissService
            .selectTExamMissList(TExamMiss.builder().cohort(cohort).examId(examId).lessonName(lessonName).build());
        long missCnt = tExamMisses.stream().map(TExamMiss::getStudentId).distinct().count();
        return StatisticsExamDTO
            .builder()
            .planExamCnt(planCnt)
            .missExamCnt(missCnt)
            .realExamCnt(planCnt - missCnt)
            .build();
    }

}
